package com.wrupple.muba.base.client.widgets.canvas;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.view.client.CellPreviewEvent;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.RangeChangeEvent;
import com.google.gwt.view.client.RangeChangeEvent.Handler;
import com.google.gwt.view.client.RowCountChangeEvent;
import com.google.gwt.view.client.SelectionModel;
import com.hydro4ge.raphaelgwt.client.Raphael;
import com.wrupple.muba.base.client.view.CanvasCell;

public class DataCanvas<T> extends Raphael implements HasData<T>{
	
	private CanvasCell<T> cell;
	private List<T> list;

	public DataCanvas(CanvasCell<T> cell,int width, int height) {
		super(width, height);
		list = new ArrayList<T>();
	}
	
	/*
	 * DRAWING
	 * 
	 */


	@Override
	public void setRowData(int start, List<? extends T> values) {
		assert start==0 : "DataCanvas can only draw items starting from the first row";
		this.list=(List<T>)values;
		cell.drawData(list,this);
	}
	
	/*
	 * SELECTION 
	 * 
	 */
	
	@Override
	public SelectionModel<? super T> getSelectionModel() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	@Override
	public void setSelectionModel(SelectionModel<? super T> selectionModel) {
		// TODO Auto-generated method stub
		
	}

	/*
	 * GARBAGE METHODS
	 */

	@Override
	public void setVisibleRange(int start, int length) {
		assert start == 0 : "DataCanvas visible range must start from zero";
		int currentLength = list.size();
		if(currentLength!=length){
			
			if(currentLength<length){
				//grow
			}else{
				//shrink
				cell.removeRowsFromIndex(length);
			}
			RangeChangeEvent.fire(this, new Range(start,length));
		}
	}


	public CanvasCell<T> getCell() {
		return cell;
	}

	public void setCell(CanvasCell<T> cell) {
		this.cell = cell;
	}

	@Override
	public void setRowCount(int count) {
		setRowCount(count, true);
	}

	@Override
	public void setRowCount(int count, boolean isExact) {
		assert isExact : "DataCanvas row count can only be exact";
		setVisibleRange(0, count);
	}
	
	@Override
	public void setVisibleRange(Range range) {
		setVisibleRange(range.getStart(), range.getLength());
	}

	@Override
	public void setVisibleRangeAndClearData(Range range,boolean forceRangeChangeEvent) {
		setVisibleRange(range);
		if(forceRangeChangeEvent){
			RangeChangeEvent.fire(this, range);
		}
	}
	
	@Override
	public Range getVisibleRange() {
		return new Range(0,getRowCount());
	}

	@Override
	public boolean isRowCountExact() {
		return true;
	}
	
	@Override
	public T getVisibleItem(int indexOnPage) {
		return list.get(indexOnPage);
	}

	@Override
	public int getVisibleItemCount() {
		return list.size();
	}
	
	@Override
	public List<T> getVisibleItems() {
		return Collections.unmodifiableList(list);
	}

	@Override
	public HandlerRegistration addCellPreviewHandler(
			com.google.gwt.view.client.CellPreviewEvent.Handler<T> handler) {
		return addHandler(handler, CellPreviewEvent.getType());
	}
	
	@Override
	public HandlerRegistration addRangeChangeHandler(Handler handler) {
		return addHandler(handler, RangeChangeEvent.getType());
	}

	@Override
	public HandlerRegistration addRowCountChangeHandler(
			com.google.gwt.view.client.RowCountChangeEvent.Handler handler) {
		return addHandler(handler, RowCountChangeEvent.getType());
	}

	@Override
	public int getRowCount() {
		return list.size();
	}



}
